// Created by WXX on 2021/12/6 9:08
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    string truncateSentence(string s, int k) {
        string res;
        for (int i = 0; i < s.size(); i++) {
            if (!k) break;
            int j = i + 1;
            while (j < s.size() && s[j] != ' ') j++;
            res += s.substr(i, j - i);
            if (k != 1) res += ' ';
            i = j;
            k--;
        }
        return res;
    }
};

int main() {

    string s = "Hello how are you Contestant";
    cout << Solution().truncateSentence(s, 4) << endl;  // Hello how are you

    return 0;
}
